package com.ryskoo.customer.customeraccount.repository;

import com.ryskoo.customer.customeraccount.domain.CustomerAccountPO;
import com.ryskoo.framework.core.repository.BasicJPAImpl;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hh
 */
@Repository
public class CustomerAccountJPAImpl extends BasicJPAImpl<CustomerAccountPO> implements ICustomerAccountRepository {

	/**
	 * 根据 customerUuids 查询customerAccount
	 * 
	 * @param customerUuids
	 * @return
	 */
	public List<CustomerAccountPO> getByCustomerUuids(List<String> customerUuids) {
		String hql = "select o from CustomerAccountPO o where o.customerUuid in (:customerUuids)";
		Query q = getH4Session().createQuery(hql);
		q.setParameterList("customerUuids", customerUuids);
		List<CustomerAccountPO> retList = q.list();
		return retList;
	}

	/**
	 * 根据 customerUuid 查询customerAccount
	 * 
	 * @param customerUuid
	 * @return
	 */
	public CustomerAccountPO getByCustomerUuid(String customerUuid) {
		String hql = "select o from CustomerAccountPO o where o.customerUuid = :customerUuid";
		Query q = getH4Session().createQuery(hql);
		q.setString("customerUuid", customerUuid);
		List<CustomerAccountPO> retList = q.list();
		if (retList != null && retList.size() > 0) {
			return retList.get(0);
		}
		return null;
	}

	/**
	 * 根据 customerUuids 删除 customerAccount
	 * 
	 * @param List<String>
	 *            needDeleteCustomerUuids
	 * @return
	 */
	public void deletesByCustomerUuids(List<String> needDeleteCustomerUuids) {
		String hql = "delete from CustomerAccountPO o where o.customerUuid in (:needDeleteCustomerUuids)";

		Map<String, Object> mapParams = new HashMap<String, Object>();
		mapParams.put("needDeleteCustomerUuids", needDeleteCustomerUuids.toArray());

		this.exeUpdate(hql, mapParams);
	}
}
